<?php

namespace app\admin\controller;
class About extends base
{

    //前置方法
    protected $beforeActionList = [
        'map' => ['only' => 'index'],
    ];

    /**
     * 搜索
     */
    protected function map()
    {
        if ($this->request->isGet()) {
            $data = $this->request->param();
            $this->datas = array();
            if (!empty($data['name'])) {
                $this->datas ['name'] = ['like', '%' . $data['name'] . '%'];
            }
        }
    }

    public function index()
    {
        $list = $this->model->page_index($this->datas)->appends($this->request->param());

        $this->assign('num', $this->model->count());
        $this->assign('list', $list);

        return $this->fetch();
    }

    /**
     * 单页面编辑
     */
    public function edit($cid)
    {
        $data = $this->model->get(['cid'=>$cid]);
        $this->assign('data', $data);
        $this->assign('cid', $cid);
        return $this->fetch();
    }

    /**
     * 保存
     */
    public function save()
    {
        if (!$this->request->isPost()) {
            return ajax_info('请求失败');
        }

        $data = $this->request->post();
        $result = $this->validate($data, 'About');
        if (true !== $result) {
            return ajax_info($result);
        }

        if (!empty($data['id'])) {
            return $this->update($data);
        }

        $res = $this->model->allowField(true)->save($data);
        if ($res) {
            return ajax_info('保存成功','y');
        } else {
            return ajax_info('添加失败');
        }

    }
}
